﻿#include <string>
#include <iostream>
#include <boost/program_options.hpp>
#include "modbus_server.hpp"

namespace po = boost::program_options;

int main(int argc, char* argv[]) {
    try {
        po::options_description desc("Modbus Slave模拟器选项");
        desc.add_options()
            ("help,h", "显示帮助信息")
            ("transport,t", po::value<std::string>()->default_value("tcp"), "传输方式 (tcp/udp)")
            ("protocol,p", po::value<std::string>()->default_value("modbus_tcp"), "协议类型 (modbus/modbus_rtu/modbus_tcp)")
            ("listen-port,l", po::value<uint16_t>()->default_value(502), "监听端口")
            ("slaves,s", po::value<std::vector<uint16_t>>()->multitoken(), "从站ID列表 (例如: 1 2 3)")
            ("data,d", po::value<std::string>()->default_value("modbus_data.bin"), "数据持久化文件")
            ;

        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, desc), vm);
        po::notify(vm);

        if (vm.count("help")) {
            std::cout << desc << std::endl;
            return 0;
        }

        // 解析传输方式
        TransportType transport;
        std::string transport_str = vm["transport"].as<std::string>();
        if (transport_str == "tcp") {
            transport = TransportType::TCP;
        }
        else if (transport_str == "udp") {
            transport = TransportType::UDP;
        }
        else {
            std::cerr << "错误的传输方式: " << transport_str << std::endl;
            return 1;
        }

        // 解析协议类型
        ProtocolType protocol;
        std::string protocol_str = vm["protocol"].as<std::string>();
        if (protocol_str == "modbus") {
            protocol = ProtocolType::MODBUS;
        }
        else if (protocol_str == "modbus_rtu") {
            protocol = ProtocolType::MODBUS_RTU;
        }
        else if (protocol_str == "modbus_tcp") {
            protocol = ProtocolType::MODBUS_TCP;
        }
        else {
            std::cerr << "错误的协议类型: " << protocol_str << std::endl;
            return 1;
        }

        uint16_t port = vm["listen-port"].as<uint16_t>();
        std::string data_file = vm["data"].as<std::string>();

        // 创建IO上下文和服务器
        boost::asio::io_context io_context;
        ModbusServer server(io_context, transport, protocol, port);

        // 加载数据
        if (!server.loadData(data_file)) {
            std::cout << "无法从文件加载数据，将使用默认配置" << std::endl;
        }

        // 添加从站
        if (vm.count("slaves")) {
            auto& slave_ids = vm["slaves"].as<std::vector<uint16_t>>();
            for (auto& slave_id : slave_ids) {
                server.addSlave(static_cast<uint8_t>(slave_id));
            }
        }
        else {
            // 默认添加从站1
            server.addSlave(1);
        }

        // 启动服务器
        server.start();

        // 等待用户输入退出
        std::cout << "输入 'quit' 或 'exit' 退出程序" << std::endl;
        std::string command;
        while (std::getline(std::cin, command)) {
            if (command == "quit" || command == "exit") {
                break;
            }
        }

        // 保存数据
        std::cout << "保存数据到文件: " << data_file << std::endl;
        server.saveData(data_file);

        // 停止服务器
        server.stop();

    }
    catch (const std::exception& e) {
        std::cerr << "错误: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}
